package sharefileclient;

import sharefileinterface.FilePart;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import sharefileinterface.ClientInterface;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author nhancom3d
 */
public class RMIClient extends UnicastRemoteObject implements ClientInterface {

    final public static int PART_SIZE = 1000000;

    public ListenRequestThread parent = null;

    private LinkedHashMap<Integer, ArrayList<FilePart>> fileShare;
    private LinkedHashMap<Integer, String> mappingFileIDName;
    sharefileinterface.ShareFileInterface server;

    public RMIClient(ListenRequestThread parent) throws RemoteException {
        super();
        this.parent = parent;
        fileShare = parent.mainThread.fileShareList;
        server = parent.mainThread.server;
        //server.hasFilePart(null, PART_SIZE, null)
    }

    public int hasPart(int fid) {
        ArrayList<FilePart> partList = fileShare.get(new Integer(fid));
        if (partList == null) {
            return 0;
        } else {
            return partList.size();
        }
    }

    @Override
    public FilePart needFilePart(int fid, int partNumber) throws RemoteException {
        mappingFileIDName = parent.mainThread.getMappingFileIDName();
        String fileGet = mappingFileIDName.get(new Integer(fid));
        if(!"".equals(fileGet)){
            parent.mainThread.prepareFilePart(fid, fileGet);
        }
        ArrayList<FilePart> partList = fileShare.get(new Integer(fid));
        FilePart iPart = new FilePart(partNumber, parent.mainThread.getmacAdd(), "", null);
        if (partList == null) {
            return iPart;
        }
        //return part
        for (FilePart part : partList) {
            if (part.partID == partNumber) {
                return part;
            }
        }
        return iPart;
    }

    @Override
    public ArrayList<FilePart> needMultiFilePart(int fid, int beginPart, int endPart) throws RemoteException {
        return new ArrayList<>();
    }

}
